package com.gxnzd.communityelderly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxnzd.communityelderly.commom.R;
import com.gxnzd.communityelderly.dao.ReqDiagnosis;
import com.gxnzd.communityelderly.dao.ResDiagnosisDrugs;
import com.gxnzd.communityelderly.dao.ResDiagnosisInfo;
import com.gxnzd.communityelderly.entity.Diagnosis;
import com.gxnzd.communityelderly.entity.Drugs;
import com.gxnzd.communityelderly.entity.Prescribe;
import com.gxnzd.communityelderly.entity.Reservation;
import com.gxnzd.communityelderly.mapper.*;
import com.gxnzd.communityelderly.service.DiagnosisService;
import com.gxnzd.communityelderly.service.DrugLibraryService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* @author 23234
* @description 针对表【diagnosis】的数据库操作Service实现
* @createDate 2024-11-11 19:34:18
*/
@Service
public class DiagnosisServiceImpl extends ServiceImpl<DiagnosisMapper, Diagnosis>
    implements DiagnosisService{

    @Resource
    private DiagnosisMapper diagnosisMapper;

    @Resource
    private ReservationMapper reservationMapper;

    @Resource
    private PrescribeMapper prescribeMapper;

    @Resource
    private DrugLibraryService drugLibraryService;

    @Resource
    private DrugsMapper drugsMapper;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 获取用户的就诊记录
     * @param userId
     * @return
     */
    @Override
    public R getUserDiagnosisList(Long userId) {
        //列表返回
        ArrayList<ResDiagnosisInfo> resList = new ArrayList<>();
        LambdaQueryWrapper<Diagnosis> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Diagnosis::getUserId,userId);
        List<Diagnosis> list = diagnosisMapper.selectList(wrapper);

        for (Diagnosis diagnosis:list) {
            ResDiagnosisInfo diagnosisInfo = new ResDiagnosisInfo();
            LambdaQueryWrapper<Prescribe> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Prescribe::getDiagnosisId,diagnosis.getDiagnosisId());
            //声明存放就诊记录中药品列表的
            ArrayList<ResDiagnosisDrugs> drugsList = new ArrayList<>();
            for (Prescribe prescribe : prescribeMapper.selectList(queryWrapper)) {
                Drugs drugs = drugsMapper.selectById(prescribe.getDrugsId());
                ResDiagnosisDrugs resDiagnosisDrugs = new ResDiagnosisDrugs();
                resDiagnosisDrugs.setDrugs(drugs);
                resDiagnosisDrugs.setPrescribe(prescribe);
                drugsList.add(resDiagnosisDrugs);
            }
            diagnosisInfo.setDiagnosis(diagnosis);
            diagnosisInfo.setDrugs(drugsList);
            resList.add(diagnosisInfo);
        }
        return R.success(resList);
    }

    /**
     * 添加就诊记录 并且修改预约表状态 (就诊结束)
     * @param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addInfo(ReqDiagnosis reqDiagnosis) {

        List<Prescribe> list = reqDiagnosis.getDrugInfo();
        //判断药品数据是否可用
        R res = drugLibraryService.setDrugLibrary(list, reqDiagnosis.getCommunityId());
        if(res.getCode() != 200){
            return res;
        }
        //对诊断数据进行填充
        Diagnosis diagnosis = new Diagnosis();
        BeanUtils.copyProperties(reqDiagnosis,diagnosis);
        //判断是否发烧
        if(reqDiagnosis.getIsFever()){
            diagnosis.setIsFever(1);
        }else {
            diagnosis.setIsFever(0);
        }
        //判断是否并发症
        if(reqDiagnosis.getIsComplication()){
            diagnosis.setIsComplication(1);
        }else{
            diagnosis.setIsComplication(1);
        }
        if(reqDiagnosis.getIsEpidemic()){
            diagnosis.setIsEpidemic(1);
        }else{
            diagnosis.setIsEpidemic(0);
        }
        //判断可用之后进行修改此预约单的状态
        Reservation reservation = reservationMapper.selectById(diagnosis.getReservationId());
        //将预约表状态进入到待支付
        reservation.setStatus("4");
        reservationMapper.updateById(reservation);
        //添加就诊信息
        diagnosisMapper.insert(diagnosis);

        //获取这条新增的信息
        LambdaQueryWrapper<Diagnosis> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Diagnosis::getReservationId,diagnosis.getReservationId());
        Diagnosis selectOne = diagnosisMapper.selectOne(wrapper);
        //获取到的新的数据将它的id添加到药品使用表
        for (Prescribe item:list) {
            item.setDiagnosisId(selectOne.getDiagnosisId());
            prescribeMapper.insert(item);
        }
        //添加进redis
        String key = diagnosis.getDiagnosisId().toString();
        redisTemplate.opsForValue().set(key,diagnosis.getDiagnosisId(),10, TimeUnit.MINUTES);

        return R.success("添加成功");
    }

    /**
     * 根据预约id获取就诊信息
     * @param reservationId
     * @return
     */
    @Override
    public R getDetails(Long reservationId) {
        //可以先判断这个预约id在预约表中的状态
        Reservation reservation = reservationMapper.selectById(reservationId);
        Integer status = Integer.parseInt(reservation.getStatus());
        if (status <= 3){
            return R.error("此服务状态没有就诊信息");
        }

        LambdaQueryWrapper<Diagnosis> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Diagnosis::getReservationId,reservationId);
        //就诊信息
        Diagnosis diagnosis = diagnosisMapper.selectOne(wrapper);
        //获取就诊的药品信息
        LambdaQueryWrapper<Prescribe> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Prescribe::getDiagnosisId,diagnosis.getDiagnosisId());
        //声明存放就诊记录中药品列表的
        ArrayList<ResDiagnosisDrugs> drugsList = new ArrayList<>();
        for (Prescribe prescribe : prescribeMapper.selectList(queryWrapper)) {
            Drugs drugs = drugsMapper.selectById(prescribe.getDrugsId());
            ResDiagnosisDrugs resDiagnosisDrugs = new ResDiagnosisDrugs();
            resDiagnosisDrugs.setDrugs(drugs);
            resDiagnosisDrugs.setPrescribe(prescribe);
            drugsList.add(resDiagnosisDrugs);
        }
//        生成最后返回的类
        ResDiagnosisInfo diagnosisInfo = new ResDiagnosisInfo();
        diagnosisInfo.setDiagnosis(diagnosis);
        diagnosisInfo.setDrugs(drugsList);
        return R.success(diagnosisInfo);
    }

    /**
     * 确认支付
     * @param id
     * @return
     */
    @Override
    public R confirmPayment(Long id) {
        Reservation reservation = reservationMapper.selectById(id);
        //支付的流程 由于没有办法模拟支付过程就直接改状态
        reservation.setStatus("6");
        reservationMapper.updateById(reservation);
        return R.success("支付成功");
    }
}




